package com.jn.book.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jn.book.entity.Books;
import com.jn.book.repository.BooksRepository;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class BooksViewModel extends ViewModel {
    private BooksRepository mRepository;
    private MutableLiveData<Books> mBooksLiveData = new MutableLiveData<>();

    public BooksViewModel() {
        mRepository = new BooksRepository();
    }

    public void getBooks(String key, String catalog_id) {
        mRepository.getBooks(key, catalog_id).subscribe(new Observer<Books>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d("asdf", "onSubscribe");
            }

            @Override
            public void onNext(@NonNull Books books) {
                Log.d("asdf", "onNext: " + books.toString());
                mBooksLiveData.postValue(books);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d("asdf", "onError");
            }

            @Override
            public void onComplete() {
                Log.d("asdf", "onComplete");
            }
        });
    }

    public LiveData<Books> getLiveData() {
        return mBooksLiveData;
    }
}